import java.util.Scanner;

public class C_1449 {
    public static void main(String[] args) {
        Scanner sc=  new Scanner(System.in);
        int n = sc.nextInt();
        int[] t = new int[n+1];
        for (int i = 1; i <= n; i++) {
            t[i] = sc.nextInt();
        }
        int max = 0;
        for (int i = n; i >0 ; i--) {
            int num= 0;
            int now = i;
            while (true){
                num++;
                now = t[now];
                if (now==0)
                    break;;
            }
            max = Math.max(num,max);
        }
        System.out.println(max);
    }
}
